/**
 * Copyright (c) 2011 Roundarch (www.roundarch.com)
 *
 * Author Joshua Jung
 *
 * ---
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.roundarch.flex.controls.graphControlClasses
{
    import com.roundarch.flex.controls.GraphControlBase;
    import com.roundarch.flex.controls.graphControlInterfaces.IInterval;
    import flash.geom.Point;

    /**
     * @author jjung
     */
    public class GraphControlUtil
    {
        //---------------------------------------------------------------------
        //
        //  Static Functions
        //
        //---------------------------------------------------------------------
        /**
         * Calculates the distance between pixels based on the provided value.
         */
        public static function calcValueWidth(value : Number, graphControl : GraphControlBase) : Number
        {
            var leftX : Number = getScreenforValue(new Point(0, 0), graphControl).x;
            var rightX : Number = getScreenforValue(new Point(value, 0), graphControl).x;

            return rightX - leftX;
        }

        /**
         * Returns the pixel height of an interval given the current graphControl settings.
         */
        public static function getPixelHeightForInterval(interval : IInterval, graphControl : GraphControlBase) : Number
        {
            var start : Point = getScreenforValue(new Point(interval.start, 0), graphControl);
            var end : Point = getScreenforValue(new Point(interval.end, 0), graphControl);

            return end.y - start.y;
        }

        /**
         * Returns the pixel width of an interval given the current graphControl settings.
         */
        public static function getPixelWidthForInterval(interval : IInterval, graphControl : GraphControlBase) : Number
        {
            var start : Point = getScreenforValue(new Point(interval.start, 0), graphControl);
            var end : Point = getScreenforValue(new Point(interval.end, 0), graphControl);

            return end.x - start.x;
        }

        /**
         * Returns the pixel value for a given data value, based on the provided GraphControl and its options.
         */
        public static function getScreenforValue(value : Point, graphControl : GraphControlBase) : Point
        {
            if (!value)
            {
                return new Point(0, 0);
            }

            var pixelLeft : Number = graphControl.padding.left;
            var pixelWidth : Number = graphControl.width - graphControl.padding.right;
            var pixelTop : Number = graphControl.padding.top;
            var pixelHeight : Number = graphControl.height - graphControl.padding.bottom;

            var hValueRange : Number = graphControl.horizontalAxisOptions.visibleMinMax.length;
            var hValueToRange : Number = (value.x - graphControl.horizontalAxisOptions.visibleMinMax.start) / hValueRange;

            var vValueRange : Number = graphControl.verticalAxisOptions.visibleMinMax.length;
            var vValueToRange : Number = (value.y - graphControl.verticalAxisOptions.visibleMinMax.start) / vValueRange;

            var x : Number = graphControl.horizontalAxisOptions.snapToPixel ? Math.round(pixelLeft + (pixelWidth * hValueToRange)) : pixelLeft + (pixelWidth * hValueToRange);
            var y : Number = graphControl.verticalAxisOptions.snapToPixel ? Math.round(pixelTop + (pixelHeight * vValueToRange)) : pixelTop + (pixelHeight * vValueToRange);

            return new Point(x, y);
        }

        public static function getValueAsString(point : Point, graphControl : GraphControlBase) : String
        {
            // TODO set this up to do vertical values too
            if (graphControl.horizontalAxisOptions && point)
            {
                return graphControl.horizontalAxisOptions.getValueAsString(point.x);
            }

            return "";
        }

        /**
         * Returns the vertical value for a pixel height.
         *
         * 'height' should be relative to the graphControl, not the stage.
         */
        public static function getValueForPixelHeight(height : Number, graphControl : GraphControlBase) : Number
        {
            var min : Number = graphControl.verticalAxisOptions.visibleMinMax.start;
            var max : Number = graphControl.verticalAxisOptions.visibleMinMax.end;

            var top : Number = 0;
            var bottom : Number = graphControl.height;

            var valsPerPixel : Number = (max - min) / (bottom - top);

            return height * valsPerPixel;
        }

        /**
         * Returns the horizontal value for a pixel width.
         *
         * 'width' should be relative to the graphControl, not the stage.
         */
        public static function getValueForPixelWidth(width : Number, graphControl : GraphControlBase) : Number
        {
            var min : Number = graphControl.horizontalAxisOptions.visibleMinMax.start;
            var max : Number = graphControl.horizontalAxisOptions.visibleMinMax.end;

            var left : Number = 0;
            var right : Number = graphControl.width;

            var valsPerPixel : Number = (max - min) / (right - left);

            return width * valsPerPixel;
        }

        /**
         * Returns the x pixel value for a given value, based on the provided GraphControl and its options.
         *
         * X should be relative to the graphControl, not the stage.
         */
        public static function getValueforScreen(pixel : Point, graphControl : GraphControlBase) : Point
        {
            var min : Number = graphControl.horizontalAxisOptions.visibleMinMax.start;
            var max : Number = graphControl.horizontalAxisOptions.visibleMinMax.end;

            var left : Number = 0;
            var right : Number = graphControl.width;

            var percent : Number = pixel.x / (right - left);

            return new Point(min + ((max - min) * percent), 0);
        }

        public static function isValidValue(value : Point, graphControl : GraphControlBase) : Boolean
        {
            if (value.x < graphControl.horizontalAxisOptions.minMax.start)
            {
                return false;
            }

            if (value.x > graphControl.horizontalAxisOptions.minMax.end)
            {
                return false;
            }

            // TODO get this to incorporate disabled ranges

            return true;
        }
    }
}
